package ca.usherbrooke.domus.sipnmove2;

import javax.swing.SwingUtilities;

import ca.usherbrooke.domus.sipnmove2.controller.phone.CallActionController;
import ca.usherbrooke.domus.sipnmove2.controller.phone.CallIdGenerator;
import ca.usherbrooke.domus.sipnmove2.controller.phone.NewCallViewController;
import ca.usherbrooke.domus.sipnmove2.controller.phone.QuitActionController;
import ca.usherbrooke.domus.sipnmove2.model.call.CallFactory;
import ca.usherbrooke.domus.sipnmove2.model.call.CallState;
import ca.usherbrooke.domus.sipnmove2.model.call.impl.CallFactoryImpl;
import ca.usherbrooke.domus.sipnmove2.model.call.impl.InitialCallState;
import ca.usherbrooke.domus.sipnmove2.model.phone.impl.PhoneImpl;
import ca.usherbrooke.domus.sipnmove2.sip.SipCallController;
import ca.usherbrooke.domus.sipnmove2.sip.SipCallIdGenerator;
import ca.usherbrooke.domus.sipnmove2.sip.SipRegister;
import ca.usherbrooke.domus.sipnmove2.sip.SipUserInformation;
import ca.usherbrooke.domus.sipnmove2.view.call.CallViewFactory;
import ca.usherbrooke.domus.sipnmove2.view.call.impl.CallViewFactoryImpl;
import ca.usherbrooke.domus.sipnmove2.view.phone.PhoneView;

/**
 * Entry point of the user agent application
 */
public class UserAgent {

	/**
	 * Transport to use under SIP
	 * 
	 * Tested with "tcp" and should work with "udp" as well
	 */
	private static final String sipTransport = "tcp";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		/*********************************************
		 * Input parameters management
		 *********************************************/
		
		if (args.length < 3) {
			System.err.println("Not enough arguments :");
			System.err.println();
			System.err.println("[command] ip_address port_number user_name");
			System.err.println("	ip_address = Local IP address");
			System.err.println("	port_number = Local port number");
			System.err.println("	user_name = SIP user name");
			System.exit(-1);
		}

		String ipAddress = args[0];
		Integer port = new Integer(args[1]);
		String userName = args[2];

		/*********************************************
		 * Objects creation and dependencies injection
		 *********************************************/

		// SIP user information
		SipUserInformation sipUserInformation = new SipUserInformation();
		sipUserInformation.setIpAddress(ipAddress);
		sipUserInformation.setPort(port);
		sipUserInformation.setName(userName);
		
		CallState initialCallState = new InitialCallState();
		
		NewCallViewController newCallViewController = new NewCallViewController();
		
			CallViewFactory callViewFactory = (CallViewFactory) new CallViewFactoryImpl();
			newCallViewController.setCallViewFactory(callViewFactory);
		
		// User agent model
		PhoneImpl phone = new PhoneImpl();
		phone.addNewCallListener(newCallViewController);
		
		// User agent view
		final PhoneView phoneView = new PhoneView();
		phoneView.setSipUserInformation(sipUserInformation);
		
			// Call action controller
			CallIdGenerator callIdGenerator = new SipCallIdGenerator();

			CallFactory callFactory = new CallFactoryImpl(initialCallState);
			
			CallActionController callActionController = new CallActionController();
			callActionController.setPhone(phone);
			callActionController.setPhoneView(phoneView);
			callActionController.setCallIdGenerator(callIdGenerator);
			callActionController.setCallFactory(callFactory);
			phoneView.setCallActionController(callActionController);

			// Quit action controller
			QuitActionController quitActionController = new QuitActionController(phone);
			phoneView.setQuitActionController(quitActionController);
		
		// SIP features
		SipRegister sipRegister = new SipRegister(ipAddress, port, sipTransport);
		
			// SIP call controller
			SipCallController sipCallController = new SipCallController(sipRegister);
			sipCallController.setPhone(phone);
			
		/*********************************************
		 * Display view
		 *********************************************/
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				phoneView.display();
			}
		});

	}

}
